<template>
    <div class="mod-role"  style="padding: 20px 10px;">
      <el-form :inline="true"  @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="pages.name" placeholder="接口名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList('1')">查询</el-button>
          <el-button  type="primary" @click="addOrUpdateHandle({'add':true})">新增</el-button>
          <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="interfaceName"
          header-align="center"
          align="center"
          label="接口名称">
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          show-overflow-tooltip
          label="接口描述">
        </el-table-column>
<!--        <el-table-column-->
<!--          prop="bodyData"-->
<!--          header-align="center"-->
<!--          align="center"-->
<!--          show-overflow-tooltip-->
<!--          label="jsonType">-->
<!--        </el-table-column>-->
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
            <el-popconfirm title="确定删除吗？" icon-color="#f00" @confirm="sendRquestDelete([scope.row.id])">
              <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
            <el-button   @click="verification(scope.row)" type="text" size="small">验证</el-button>

          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right;padding-top: 10px;">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

      <el-dialog
      width="45%"
      :title="titleStr"
      :visible.sync="editPostJsonFlag"
      :close-on-click-modal="false"
      append-to-body>
      <div v-if="editPostJsonFlag">
        <vue-json-editor
          v-model="resultInfo"
          :showBtns="false"
          :mode="'code'"
          @json-change="onJsonChange"
          @json-save="onJsonSave"
          @has-error="onError"
        />
        <br>
      <el-button :disabled="showloading" :type="!showloading?'primary':'success'" @click="checkJson">
        {{ !showloading?'确定':'等待……' }}
      </el-button>
      <div v-if="resultInfo2!=''" style="max-height:250px;overflow-y: scroll;border:2px solid #eee;margin:10px 0 0 0 ">
        <pre> {{ resultInfo2 }}</pre>
      </div>
    </div>
    </el-dialog>
    </div>
  </template>

  <script>
      import request from '@/utils/request'
    import vueJsonEditor from 'vue-json-editor'
    import AddOrUpdate from './role-add-or-update'
    export default {
      data () {
        return {
          showloading:false,
          queryStr:"",
          titleStr:"JSON参数验证",
          resultInfo2:"",
          editPostJsonFlag: false,
          resultInfo:{},
          dataList: [],
          pageIndex: 1,
          pageSize: 10,
          hasJsonFlag:true,  // json是否验证通过
          totalPage: 0,
          dataListLoading: false,
          dataListSelections: [],
          addOrUpdateVisible: false,
          pages:{
            "name": "",
            "page": 1,
            "limit": 10
          }
        }
      },
      components: {
        AddOrUpdate,vueJsonEditor
      },
      created () {
        this.getDataList()
      },
      methods: {
        onJsonChange (value) {
          this.onJsonSave(value)
        },
        onJsonSave (value) {
          this.resultInfo = value
          this.hasJsonFlag = true
        },
        onError(value) {
          this.hasJsonFlag = false
        },
        verification(v){
          this.resultInfo = {}
          this.queryStr = v.interfaceName
          this.resultInfo2 =  ''
          this.editPostJsonFlag = true
        },
        // 检查json
      checkJson(){
        this.showloading = true
        request({
            url: '/ht-datadevelop/table/jsondata/asyncRequestApi/'+this.queryStr,
            method: 'post',
            data: this.resultInfo
          }).then((res) => {
            this.showloading = false
            if(res.code==200){
              this.resultInfo2 = res.data.dataInfo||'无数据'
            }
          })
      },
        // 获取数据列表
        getDataList (v) {
          this.pages.page = v=='1'?'1':this.pages.page+''
          this.pages.limit = this.pages.limit+''
          this.dataListLoading = true
          request({
            url: '/ht-datadevelop/table/jsondata/list',
            method: 'post',
            data: this.pages
          }).then((res) => {
            if (res.code === 200) {
              this.dataList = res.data.list
              this.totalPage = res.data.totalCount
            } else {
              this.dataList = []
              this.totalPage = 0
            }
            this.dataListLoading = false
          })
        },
        // 每页数
        sizeChangeHandle (val) {
        //   this.pageSize = val
          this.pages.limit = val
          this.pageIndex = 1
          this.getDataList()
        },
        // 当前页
        currentChangeHandle (val) {
        //   this.pageIndex = val
          this.pages.page = val
          this.getDataList()
        },
        // 多选
        selectionChangeHandle (val) {
          this.dataListSelections = val
        },
        // 新增 / 修改
        addOrUpdateHandle (row) {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(row)
          })
        },
        sendRquestDelete(ids){
          request({
              url: '/ht-datadevelop/table/jsondata/deleteByIds',
              method: 'post',
              data:{ids:ids}
            }).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
        },
        // 删除
        deleteHandle (id) {
          var ids =this.dataListSelections.map(item => {
            return item.id
          })
          this.$confirm(`确定${id ? '删除' : '批量删除'}?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.sendRquestDelete(ids)
          }).catch(() => {})
        }
      }
    }
  </script>
